package com.baomibing.flow.express;

import lombok.AllArgsConstructor;
import lombok.Getter;

import javax.script.*;
import java.util.Map;

/**
 * Expression
 *
 * @author frog 2023/9/18 16:00
 * @version 1.0.0
 **/
@AllArgsConstructor
public class Expression {


    @Getter
    private String express;


    public boolean verify(Map<String, Object> variables) {

        try {
            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine se = sem.getEngineByName("JavaScript");
            Bindings bindings = new SimpleBindings();
            bindings.putAll(variables);
            return Boolean.TRUE.equals(se.eval(express, bindings));
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }

    }
}
